package cn.gz.demo.ui;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;

import androidx.databinding.DataBindingUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cn.gz.demo.R;
import cn.gz.demo.databinding.DatePickerBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.LogUtil;
import cn.gz.demo.view.CustomDatePickerAlertDialog;
import cn.gz.demo.view.date.CustomDatePicker;

/**
 * Created by Liuyz on 2018/1/2.
 * com.contrarywind:Android-PickerView:4.1.9
 */
public class DatePickerActivity extends BaseActivity implements View.OnClickListener {
    private DatePickerBinding binding;
    int nowYear = 2016;
    int nowMonth = 10;
    int nowDay = 8;
    private String dateStr, mouth1, day1;
    private LinearLayout main;

    private static final List<String> options1Items = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_date_picker);

        binding.datePickerDate.setOnClickListener(this);
        binding.datePickerTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.date_picker_date) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(this, 2, new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    nowYear = year;
                    nowMonth = monthOfYear;
                    nowDay = dayOfMonth;
                }
            }, 2016, 10, 8);
            datePickerDialog.show();
            showDate();
        } else if (id == R.id.date_picker_time) {
            showDateAndTimePickerDialog();
        }
    }

    // 显示选择日期
    private void showDate() {
        binding.datePickerDate.setText("你选择的日期是：" + nowYear + "年" + nowMonth + "月" + nowDay + "日");
    }

    private void showDateDialog() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        //获取当前时间
        String now = sdf.format(new Date());
        CustomDatePicker customDatePicker = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                Log.d("Liuyz", time);
                binding.datePickerDate.setText(time.split(" ")[0]);

            }
        }, "1990-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker.showSpecificTime(false); // 不显示时和分
        customDatePicker.setIsLoop(false); // 不允许循环滚动

        SimpleDateFormat sdf_1 = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        //获取当前时间
        String now_1 = sdf_1.format(new Date());
        customDatePicker.show(now_1);
    }

    private void showDateAndTimePickerDialog() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        CustomDatePickerAlertDialog dialog = new CustomDatePickerAlertDialog(context, year, month, day);
        dialog.setPositiveButton("确定", new CustomDatePickerAlertDialog.AntDatePickerDialogClickListener() {
            @Override
            public void onClick(View view, int year, int month, int day) {
                LogUtil.showLog("你设置的日期是：" + year + "/" + month + "/" + day);
            }
        });
        dialog.setNegativeButton("取消", null);
        dialog.setCancelable(false);
        dialog.show();
    }
}
